package com.leetcode;

/**
 * 14. 最长公共前缀
 * 纵向扫描
 */
public class Solution14 {

    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        int length = Integer.MAX_VALUE;
        for (String str : strs) {
            length = Math.min(length, str.length());
        }
        for (int i = 0; i < length; i++) {
            char ch = strs[0].charAt(i);
            boolean eq = true;
            for (int j = 1; j < strs.length; j++) {
                if (ch != strs[j].charAt(i)) {
                    eq = false;
                    break;
                }
            }
            if (eq) {
                sb.append(ch);
            } else {
                return sb.toString();
            }
        }
        return sb.toString();
    }

}
